

<script type="text/javascript" language="Javascript">
  $(document).ready(function() {
      reset_form();
      $("#flex-grid-table").flexigrid({

          url: "${resource(dir:'answeringType', file:'list')}",
          dataType: 'json',
          colModel : [
              {display: 'SL', name : 'sl', width : 150, sortable : true, align: 'center',process:executeEditAnsweringType},
              {display: 'ID', name : 'id', width : 180, sortable : true, align: 'left',hide: true,process:executeEditAnsweringType},
              {display: 'Code', name : 'code', width : 150, sortable : true, align: 'center' ,process:executeEditAnsweringType},
              {display: 'Name', name : 'name', width : 180, sortable : true, align: 'left',process:executeEditAnsweringType}
          ],
          buttons : [
          ],

          searchitems : [
              {display: 'Code', name : 'code'},
              {display: 'Name', name : 'name', isdefault: true}
          ],
          sortname: "name",
          sortorder: "asc",
          usepager: true,
          title: 'All QuestionType Information',
          useRp: true,
          rp: 15,
          showTableToggleBtn: true,
          width: 800,
          height: 200,
          singleSelect:true
      });

  });
  function getSelectedAnsweringTypeId()
  {
      var items = $('.trSelected');
      var answeringTypeInstanceId = '';
      for(i=0;i<items.length;i++){
          if(i == items.length-1){
              answeringTypeInstanceId += items[i].id.substr(3);
          }else{
              answeringTypeInstanceId += items[i].id.substr(3)+", ";
          }

      }
      return answeringTypeInstanceId;

  }
  function executePreConditionAnsweringType() {
      // trim field vales before process.
//      trim_form();
//      if ($("#gFormAnsweringType").validate().form({onfocusout: false}) == false) {
//        return false;
//      }
      return true;
  }
  function executeAjaxAnsweringType() {
    if(!executePreConditionAnsweringType()) {
      return false;
    }
    var actionUrl = null;
    if ($('#gFormAnsweringType input[name = id]').val()) {
      actionUrl = "${request.contextPath}/${params.controller}/update";
    } else {
      actionUrl = "${request.contextPath}/${params.controller}/create";
    }
    jQuery.ajax({
      type:'post',
      data:jQuery("#gFormAnsweringType").serialize(),
      url: actionUrl,
      success:function(data, textStatus) {
          executePostConditionAnsweringType(data);
      },
      error:function(XMLHttpRequest, textStatus, errorThrown) {
      },
      complete:function(){
      },
      dataType:'json'
    });
    return false;
  }
  function executePostConditionAnsweringType(result) {
      if (result.type == 1) {
          $("#flex-grid-table").flexReload();
          reset_form('#gFormAnsweringType');
      }
      MessageRenderer.render(result);
  }
  function deleteAjaxAnsweringType() {    // Delete record
    var answeringTypeInstanceId = getSelectedAnsweringTypeId();
    if(executePreConditionForDeleteAnsweringType(answeringTypeInstanceId))
    {

        $.ajax({
            type: "POST",
            dataType: "json",
            data:jQuery("#gFormAnsweringType").serialize(),
            url: "${resource(dir:'answeringType', file:'delete')}",
            success: function(message) {
                executePostConditionForDeleteAnsweringType(message);
            }
        });

      %{--$("#dialog").dialog("destroy");--}%
      %{--$("#dialog-confirm-answeringTypeInstance").dialog({--}%
        %{--resizable: false,--}%
        %{--height:150,--}%
        %{--modal: true,--}%
        %{--title: 'Delete alert',--}%
        %{--buttons: {--}%
          %{--'Delete item(s)': function() {--}%
            %{--$(this).dialog('close');--}%
            %{--$.ajax({--}%
              %{--type: "POST",--}%
              %{--dataType: "json",--}%
              %{--data:jQuery("#gFormAnsweringType").serialize(),--}%
              %{--url: "${resource(dir:'answeringTypeInstance', file:'delete')}",--}%
              %{--success: function(message) {--}%
                %{--executePostConditionForDeleteAnsweringType(message);--}%
              %{--}--}%
            %{--});--}%
          %{--},--}%
          %{--Cancel: function() {--}%
            %{--$(this).dialog('close');--}%
          %{--}--}%
        %{--}--}%
      %{--}); //end of dialog--}%
    }
  }

  function executePreConditionForEditAnsweringType(answeringTypeInstanceId) {
    if(answeringTypeInstanceId == null)
    {
      alert("Please select a answeringTypeInstance to edit") ;
      return false;
    }
    return true;
  }
  function executeEditAnsweringType(celDiv, answeringTypeInstanceId) {

      $( celDiv ).click( function() {
          if(answeringTypeInstanceId)
          {
              $.ajax({
                  type: "POST",
                  url: "${resource(dir:'answeringType', file:'edit')}?id="+ answeringTypeInstanceId,
                  success: function(entity) {
                      executePostConditionForEditAnsweringType(entity);
                  },
                  dataType:'json'
              });
          }
      });
  }
  function executePostConditionForEditAnsweringType(data) {
      if (data == null) {
        alert('Selected answeringTypeInstance might have been deleted by someone');  //Message renderer
      } else {
        showAnsweringType(data);
      }
  }
  function executePreConditionForDeleteAnsweringType(answeringTypeInstanceId) {
    if(answeringTypeInstanceId == null)
    {
      alert("Please select a answeringTypeInstance to delete") ;
      return false;
    }
    return true;
  }
  function executePostConditionForDeleteAnsweringType(data) {
      if (data.type ==1) {
          $("#flex-grid-table").flexReload();
         reset_form('#gFormAnsweringType');
      }
      MessageRenderer.render(data)
  }
  function showAnsweringType(entity) {
    $('#gFormAnsweringType input[name = id]').val(entity.id);
    $('#gFormAnsweringType input[name = version]').val(entity.version);
    
    $('#code').val(entity.code);
    $('#name').val(entity.name);
    $('#create-button').attr('value', 'Update');
     $('#cancel-button').attr('value', 'Cancel');
    $('#delete-button').show();
  }
  function executeSearchAnsweringType(fieldName, fieldValue){
    if(executePreConditionForSearchAnsweringType(fieldName, fieldValue))
    {
      $.ajax({
        type: "POST",
        url: "${resource(dir:'answeringTypeInstance', file:'search')}?fieldName="+fieldName+"&fieldValue="+fieldValue,
        success: function(entity) {
          executePostConditionForSearchAnsweringType(entity, fieldName, fieldValue);
        },
        dataType:'json'
      });
    }
  }
  function executePreConditionForSearchAnsweringType(fieldName,fieldValue) {
      // trim field vales before process.
      $('#'+fieldName).val($.trim($('#'+fieldName).val()));
      if(fieldValue == '' || fieldValue == null){
          reset_form("#gFormAnsweringType");
          return false;
      }
      return true;
  }
  function executePostConditionForSearchAnsweringType(data, fieldName, fieldValue) {
      if (data == null) {
          reset_form("#gFormAnsweringType"); // Clear Form
          $('#'+fieldName).val(fieldValue); // Set search field with fieldValue
      } else {
        showAnsweringType(data);
      }
  }
</script>